package dao;

import entity.MeetingRoom;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MeetingRoomDao extends BaseDao {
    // 1. 查询所有会议室（按容纳人数降序）
    public List<MeetingRoom> findAll() {
        List<MeetingRoom> roomList = new ArrayList<>();
        Connection conn = getConnection();
        System.out.println("数据库连接是否成功：" + (conn != null ? "成功" : "失败"));
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM meetingRoom ORDER BY number DESC";

        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                MeetingRoom room = new MeetingRoom();
                room.setId(rs.getInt("id"));
                room.setName(rs.getString("name"));
                room.setNumber(rs.getInt("number"));
                room.setDept(rs.getString("dept"));
                room.setLocal(rs.getString("local"));
                room.setState(rs.getInt("state"));
                roomList.add(room);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs);
        }
        return roomList;
    }

    // 2. 添加会议室
    public boolean add(MeetingRoom room) {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        String sql = "INSERT INTO meetingRoom (name, number, dept, local, state) VALUES (?, ?, ?, ?, ?)";

        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, room.getName());
            pstmt.setInt(2, room.getNumber());
            pstmt.setString(3, room.getDept());
            pstmt.setString(4, room.getLocal());
            pstmt.setInt(5, room.getState());
            return pstmt.executeUpdate() > 0; // 影响行数>0则添加成功
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(null);
        }
        return false;
    }

    // 3. 删除会议室（按id）
    public boolean deleteById(Integer id) {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        String sql = "DELETE FROM meetingRoom WHERE id = ?";

        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(null);
        }
        return false;
    }
    // 按名称模糊查询（新增方法）
    public List<MeetingRoom> findByName(String name) {
        List<MeetingRoom> roomList = new ArrayList<>();
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        // 模糊查询SQL（忽略大小写）
        String sql = "SELECT * FROM meetingRoom WHERE name LIKE ? ORDER BY number DESC";

        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "%" + name + "%"); // 拼接模糊查询符
            rs = pstmt.executeQuery();
            while (rs.next()) {
                MeetingRoom room = new MeetingRoom();
                room.setId(rs.getInt("id"));
                room.setName(rs.getString("name"));
                room.setNumber(rs.getInt("number"));
                room.setDept(rs.getString("dept"));
                room.setLocal(rs.getString("local"));
                room.setState(rs.getInt("state"));
                roomList.add(room);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs);
        }
        System.out.println(roomList);
        return roomList;
    }
}
